﻿using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.NetWork.Base;
using HR60_PanelSoft.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HR60_PanelSoft.NetWork.Sender
{
    class Sender_0105:BaseSender
    {


         // device.getConnnectedPins()判断，如果没有连接的设备，不处理
        public Sender_0105(byte panelNo, byte heatNum, Device subDevice, Device device)
        {





            if (device.type.exType != ExDeviceType.CentralHeating || StringUils.isBlack(subDevice.nickName))
            {
                throw new Exception("数据内容有问题,请查看地暖属性是否配置正确？");
            }


            byte p = 0;//面板
            byte n = 0;//网络
            {


                Pin pin = null;
                if (!CollectionUtils.isBlank(subDevice.getConnnectedPins()))
                {
                    pin = subDevice.getConnnectedPins()[0];

                }
                else if (!CollectionUtils.isBlank(device.childDevices))
                {
                    if (!CollectionUtils.isBlank(device.childDevices[0].getConnnectedPins()))
                        pin = device.childDevices[0].getConnnectedPins()[0];
                }


                if (pin == null)
                {
                    throw new Exception("数据内容有问题,请查看地暖属性没有关联负载,请检查配置？");
                }
                object[] netAndPanle = pin.getPanelAndNet();
                Panel panel = (Panel)netAndPanle[0];
                Net net = (Net)netAndPanle[1];

                if (panel == null || net == null)
                {
                    throw new Exception("数据内容有问题,请查看地暖属性是否配置正确？");
                }

                p = (byte)panel.index;
                n = (byte)net.index;


            }

            byte[] data = new byte[17]; //



            string binaryString = "1";

            binaryString = binaryString + "0";//0:不锁定

            binaryString = binaryString + "0";
            binaryString = binaryString + "0";

            string nickName = subDevice.nickName;
          
            byte[] chinaByte = ByteUils.china2Hex(nickName);
            int nickLength = chinaByte.Length;
            if (chinaByte.Length > 14)
            {
                nickLength = 14;
            }

            binaryString = binaryString + ByteUils.byte2Binary((byte)nickLength, 4);


            int cur = 0;
            data[cur++] = Convert.ToByte(binaryString, 2);

          

            int i = 0;
            for (; i < nickLength; i++)
            {
                data[cur++] = chinaByte[i];
            }




            //协议和温控器型




            data[15] = n;    //网络号
            data[16] = p;    //面板号



            this.package = Package.getPackage(0x01, 0x05, panelNo, heatNum, data);

        }

    }
}
